package com.howie.hmvc.paramparse;

import java.lang.annotation.Annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.howie.hmvc.annotations.Valid;
import com.howie.hmvc.exception.ArguErrorException;

public class ParamInteger extends AbsParamParse{

	private boolean isValid;
	private boolean required;
	private double minVal;
	private double maxVal;
	
	@Override
	public void init(Annotation[] an) throws Exception {
		Valid valid = getValid(an);
		if(valid != null){
			isValid = true;
			required = valid.required();
			minVal = valid.minVal();
			maxVal = valid.maxVal();
		}
	}
	
	@Override
	public Object getParamter(HttpServletRequest request,HttpServletResponse response)throws Exception{
		Integer val = null;
		try{
			val = Integer.parseInt(request.getParameter(name));
		}catch(Exception e){
			LOG.debug("参数["+name+"]解析出错");
		}
		if(isValid){
			if(val == null){
				if(required){
					throw new ArguErrorException(name,"required",null);
				}
			}else{
				if(val < minVal){
					throw new ArguErrorException(name,"minVal",minVal);
				}
				if(val > maxVal){
					throw new ArguErrorException(name,"maxVal",maxVal);
				}
			}
		}
		return val;
	}
	
}
